import { _decorator, Component, Node, Enum, EventHandler, log } from 'cc';
import { DEV } from 'cc/env';
import { KeyBoardEvent } from '../KeyboardDefine';
import { KeyEventListener } from '../../core/GameInputListeners';
import { KeyBoardComponent } from '../../cocos/component/KeyBoardComponent';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputEvent from '../../core/GameInputEvent';
import GEvent from '../../../cfw/event/GEvent';

const { ccclass, property } = _decorator;

/**
 * 按键组件
 */
@ccclass('KeyCodeHandler')
export class KeyCodeHandler extends KeyBoardComponent implements KeyEventListener {

    @property({ type: Enum(InputKeyCode) })
    keyCode: InputKeyCode = InputKeyCode.NONE;

    @property(EventHandler)
    handler: EventHandler = new EventHandler();

    @property({ tooltip: DEV && "音效ID" })
    @property
    soundEventId: number = 0;

    @property({ tooltip: DEV && "是否持续调用" })
    @property
    isContinuous: boolean = false;

    @property({ tooltip: DEV && "是否监听键盘的抬起事件" })
    @property
    hasUpEvent: boolean = false;


    @property({ tooltip: DEV && "持续条用的间隔时间" })
    @property
    continuousDuration: number = 0;

    _isBelongKeyBoard: boolean = false;

    set isBelongKeyBoard(f: boolean) {
        this._isBelongKeyBoard = f;
        // Debug.log(' this.isBelongKeyBoard ', this.isBelongKeyBoard)
        this.keyProcessor.isBelongKeyBoard = this.isBelongKeyBoard;
    }

    get isBelongKeyBoard() {
        return this._isBelongKeyBoard
    }

    private _isActive: boolean = false;
    private _isClicked: boolean = false;
    protected isTouch: boolean = false;
    private _event: GameInputEvent;

    private _tempTime: number = 0;


    start() {
        if (this.isContinuous) {
            this.continuousDuration = 0.3;
        }
    }
    setHandler(node: Node, component: string, hname: string) {
        if (this.handler) {
            this.handler.target = node;
            this.handler.component = component;
            this.handler.handler = hname;
        } else {
            log(' handler is null ')
        }

    }

    isBlockInput(): boolean {
        return this._isClicked;
    }

    onKeyBoardPress(keyCode: InputKeyCode, event?: GameInputEvent): void {
        this._isClicked = false;
        if (this.keyCode == InputKeyCode.NONE || this.keyCode != keyCode || this.handler.target == undefined) {
            return;
        }

        this._tempTime = 0;
        this._isClicked = true;
        this.isTouch = true;
        event.type = Node.EventType.TOUCH_START
        EventHandler.emitEvents([this.handler], event)
        this._event = event;
        this.sound();
    }

    sound() {
        if (this.soundEventId != 0) {
            GEvent.instance().emit(KeyBoardEvent.PLAY_AUDIO, this.soundEventId)
        }
    }

    onKeyBoardRelease(keyCode: InputKeyCode, event?: GameInputEvent): void {

        this._isClicked = false;
        if (this.keyCode == InputKeyCode.NONE || this.keyCode != keyCode || this.handler.target == undefined) {
            return;
        }

        this._event = null;
        this._isClicked = true;
        this.isTouch = false;
        if (this.hasUpEvent) {
            event.type = Node.EventType.TOUCH_END
            EventHandler.emitEvents([this.handler], event)
        }
    }

    update(dt: number) {

        if (this.isTouch) {
            // LogUtils.log(' this.isContinuous && this._isClicked ',this.isContinuous , this._isClicked,this._tempTime,this.keyCode)
            if (this.continuousDuration > 0) {
                this._tempTime += dt;
                if (this._tempTime >= this.continuousDuration) {
                    this._tempTime = 0;
                    EventHandler.emitEvents([this.handler], this._event)
                }
            }
        }
    }

    onEnable() {
        this.keyProcessor.addKeyEventListener(this);
    }

    onDisable() {
        this.keyProcessor.removeKeyEventListener(this);
    }
}